//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CTSMMCapletAlphaFormCalibration.h"
using namespace Cephei::QL::Models::Marketmodels::Models;
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/PiecewiseConstantCorrelation.h>
#include <gen/QL/Models/Marketmodels/Models/PiecewiseConstantVariance.h>
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/Models/AlphaForm.h>
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Models/Marketmodels/Models/CTSMMCapletCalibration.h>
using namespace Cephei::QL::Models::Marketmodels;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletAlphaFormCalibration::CCTSMMCapletAlphaFormCalibration (Cephei::QL::Models::Marketmodels::IEvolutionDescription^ evolution, Cephei::QL::Models::Marketmodels::IPiecewiseConstantCorrelation^ corr, Cephei::Core::IVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ displacedSwapVariances, Cephei::Core::IVector<Double>^ capletVols, Cephei::QL::Models::Marketmodels::ICurveState^ cs, Double displacement, Cephei::Core::IVector<Double>^ alphaInitial, Cephei::Core::IVector<Double>^ alphaMax, Cephei::Core::IVector<Double>^ alphaMin, Boolean maximizeHomogeneity, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Models::Marketmodels::Models::IAlphaForm^>^ parametricForm) : CCTSMMCapletCalibration(CCTSMMCapletAlphaFormCalibration::typeid)
{
    CEvolutionDescription^ _Cevolution;
    CPiecewiseConstantCorrelation^ _Ccorr;
    CCurveState^ _Ccs;
    CAlphaForm^ _CparametricForm;
    try
    {
#ifdef HANDLE
        _phCTSMMCapletAlphaFormCalibration = NULL;
#endif
        _Cevolution = safe_cast<CEvolutionDescription^> (evolution);
        _Cevolution->Lock();
        QuantLib::EvolutionDescription& _evolution = static_cast<QuantLib::EvolutionDescription&> (_Cevolution->GetReference ()); 
        _Ccorr = safe_cast<CPiecewiseConstantCorrelation^> (corr);
        _Ccorr->Lock();
        boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation>& _corr = static_cast<boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation>&> (_Ccorr->GetShared ()); 
        displacedSwapVariances ->Lock ();
        INativeVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ _NCIdisplacedSwapVariances = displacedSwapVariances->getFeature (NativeFeature::shared_ptr);
        CPiecewiseConstantVarianceVector^ _NCdisplacedSwapVariances = safe_cast<CPiecewiseConstantVarianceVector^>(_NCIdisplacedSwapVariances);
        std::vector<boost::shared_ptr<
                        QuantLib::PiecewiseConstantVariance> >& _displacedSwapVariances = static_cast<std::vector<boost::shared_ptr<
                        QuantLib::PiecewiseConstantVariance> >&> (_NCdisplacedSwapVariances->GetShared ());
        capletVols->Lock();
        INativeVector<Double>^ _NCIcapletVols = capletVols->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCcapletVols = safe_cast<CDoubleVector^>(_NCIcapletVols);
        std::vector<QuantLib::Volatility>& _capletVols = static_cast<std::vector<QuantLib::Volatility>&> (_NCcapletVols->GetReference ());
        _Ccs = safe_cast<CCurveState^> (cs);
        _Ccs->Lock();
        boost::shared_ptr<QuantLib::CurveState>& _cs = static_cast<boost::shared_ptr<QuantLib::CurveState>&> (_Ccs->GetShared ()); 
        QuantLib::Spread _displacement = (QuantLib::Spread)ValueHelper::Convert (displacement); //d
        alphaInitial->Lock();
        INativeVector<Double>^ _NCIalphaInitial = alphaInitial->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCalphaInitial = safe_cast<CDoubleVector^>(_NCIalphaInitial);
        std::vector<QuantLib::Real>& _alphaInitial = static_cast<std::vector<QuantLib::Real>&> (_NCalphaInitial->GetReference ());
        alphaMax->Lock();
        INativeVector<Double>^ _NCIalphaMax = alphaMax->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCalphaMax = safe_cast<CDoubleVector^>(_NCIalphaMax);
        std::vector<QuantLib::Real>& _alphaMax = static_cast<std::vector<QuantLib::Real>&> (_NCalphaMax->GetReference ());
        alphaMin->Lock();
        INativeVector<Double>^ _NCIalphaMin = alphaMin->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCalphaMin = safe_cast<CDoubleVector^>(_NCIalphaMin);
        std::vector<QuantLib::Real>& _alphaMin = static_cast<std::vector<QuantLib::Real>&> (_NCalphaMin->GetReference ());
        bool _maximizeHomogeneity = (bool)ValueHelper::Convert (maximizeHomogeneity); //d
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Models::Marketmodels::Models::IAlphaForm^>::IsSome::get (parametricForm))
        {
            _CparametricForm = safe_cast<CAlphaForm^> (parametricForm->Value);
            _CparametricForm->Lock();
        }
        boost::shared_ptr<QuantLib::AlphaForm> _parametricForm = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Models::Marketmodels::Models::IAlphaForm^>::IsSome::get (parametricForm) ? static_cast<boost::shared_ptr<QuantLib::AlphaForm>> (_CparametricForm->GetShared ()) : boost::shared_ptr<QuantLib::AlphaForm>()); //1
        _ppCTSMMCapletAlphaFormCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletAlphaFormCalibration> (new QuantLib::CTSMMCapletAlphaFormCalibration ( _evolution,  _corr,  _displacedSwapVariances,  _capletVols,  _cs,  _displacement,  _alphaInitial,  _alphaMax,  _alphaMin,  _maximizeHomogeneity,  _parametricForm ));
        SetCTSMMCapletCalibration (boost::dynamic_pointer_cast<QuantLib::CTSMMCapletCalibration> (*_ppCTSMMCapletAlphaFormCalibration));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cevolution != nullptr) _Cevolution->Unlock();
        if (_Ccorr != nullptr) _Ccorr->Unlock();
        if (displacedSwapVariances != nullptr) displacedSwapVariances->Unlock();    //not optional
        if (capletVols != nullptr) capletVols->Unlock();    //not optional
        if (_Ccs != nullptr) _Ccs->Unlock();
        if (alphaInitial != nullptr) alphaInitial->Unlock();    //not optional
        if (alphaMax != nullptr) alphaMax->Unlock();    //not optional
        if (alphaMin != nullptr) alphaMin->Unlock();    //not optional
        if (_CparametricForm != nullptr) _CparametricForm->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletAlphaFormCalibration::CCTSMMCapletAlphaFormCalibration (boost::shared_ptr<QuantLib::CTSMMCapletAlphaFormCalibration>& childNative, Object^ owner) : CCTSMMCapletCalibration(CCTSMMCapletAlphaFormCalibration::typeid)
{
#ifdef HANDLE
	_phCTSMMCapletAlphaFormCalibration = NULL;
#endif
	_ppCTSMMCapletAlphaFormCalibration = &childNative;
    _ppCTSMMCapletCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletCalibration> (boost::dynamic_pointer_cast<QuantLib::CTSMMCapletCalibration> (*_ppCTSMMCapletAlphaFormCalibration));
}
Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletAlphaFormCalibration::CCTSMMCapletAlphaFormCalibration (QuantLib::CTSMMCapletAlphaFormCalibration& childNative, Object^ owner) : CCTSMMCapletCalibration(CCTSMMCapletAlphaFormCalibration::typeid)
{
#ifdef HANDLE
	_phCTSMMCapletAlphaFormCalibration = NULL;
#endif
	_ppCTSMMCapletAlphaFormCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletAlphaFormCalibration> (&childNative);
    _ppCTSMMCapletCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletCalibration> (boost::dynamic_pointer_cast<QuantLib::CTSMMCapletCalibration> (*_ppCTSMMCapletAlphaFormCalibration));
    _CTSMMCapletAlphaFormCalibrationOwner = owner;
    _CTSMMCapletCalibrationOwner = owner;
}

Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletAlphaFormCalibration::CCTSMMCapletAlphaFormCalibration (CCTSMMCapletAlphaFormCalibration^ copy) : CCTSMMCapletCalibration(CCTSMMCapletAlphaFormCalibration::typeid)
{
#ifdef HANDLE
	_phCTSMMCapletAlphaFormCalibration = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCTSMMCapletAlphaFormCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletAlphaFormCalibration> (copy->GetShared());
        _ppCTSMMCapletCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletCalibration> (boost::dynamic_pointer_cast<QuantLib::CTSMMCapletCalibration> (*_ppCTSMMCapletAlphaFormCalibration));
    }
}
Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletAlphaFormCalibration::CCTSMMCapletAlphaFormCalibration (PLATFORM::Type^ t) : CCTSMMCapletCalibration(CCTSMMCapletAlphaFormCalibration::typeid)
{
#ifdef HANDLE
	_phCTSMMCapletAlphaFormCalibration = NULL;
#endif
	if (!t->IsSubclassOf(CCTSMMCapletAlphaFormCalibration::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletAlphaFormCalibration::CCTSMMCapletAlphaFormCalibration (QuantLib::Handle<QuantLib::CTSMMCapletAlphaFormCalibration>& childNative, Object^ owner)  : CCTSMMCapletCalibration(CCTSMMCapletAlphaFormCalibration::typeid)
{
	_phCTSMMCapletAlphaFormCalibration = &childNative;
	_ppCTSMMCapletAlphaFormCalibration = &static_cast<boost::shared_ptr<QuantLib::CTSMMCapletAlphaFormCalibration>>(childNative.currentLink());
    _ppCTSMMCapletCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletCalibration> (boost::dynamic_pointer_cast<QuantLib::CTSMMCapletCalibration> (*_ppCTSMMCapletAlphaFormCalibration));
    _CTSMMCapletAlphaFormCalibrationOwner = owner;
}
Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletAlphaFormCalibration::CCTSMMCapletAlphaFormCalibration (QuantLib::Handle<QuantLib::CTSMMCapletAlphaFormCalibration> childNative)  : CCTSMMCapletCalibration(CCTSMMCapletAlphaFormCalibration::typeid)
{
	_phCTSMMCapletAlphaFormCalibration = &childNative;
	_ppCTSMMCapletAlphaFormCalibration = &static_cast<boost::shared_ptr<QuantLib::CTSMMCapletAlphaFormCalibration>>(childNative.currentLink());
    _ppCTSMMCapletCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletCalibration> (boost::dynamic_pointer_cast<QuantLib::CTSMMCapletCalibration> (*_ppCTSMMCapletAlphaFormCalibration));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletAlphaFormCalibration::CCTSMMCapletAlphaFormCalibration (QuantLib::CTSMMCapletAlphaFormCalibration childNative)  : CCTSMMCapletCalibration(CCTSMMCapletAlphaFormCalibration::typeid)
{
#ifdef HANDLE
	_phCTSMMCapletAlphaFormCalibration = NULL;
#endif
	_ppCTSMMCapletAlphaFormCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletAlphaFormCalibration> (new QuantLib::CTSMMCapletAlphaFormCalibration (childNative));
    _ppCTSMMCapletCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletCalibration> (boost::dynamic_pointer_cast<QuantLib::CTSMMCapletCalibration> (*_ppCTSMMCapletAlphaFormCalibration));
}
#endif

Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletAlphaFormCalibration::~CCTSMMCapletAlphaFormCalibration ()
{
    if (_ppCTSMMCapletAlphaFormCalibration != NULL)
    {
	    delete _ppCTSMMCapletAlphaFormCalibration;
        _ppCTSMMCapletAlphaFormCalibration = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletAlphaFormCalibration::!CCTSMMCapletAlphaFormCalibration ()
{
    if (_ppCTSMMCapletAlphaFormCalibration != NULL)
    {
	    delete _ppCTSMMCapletAlphaFormCalibration;
    }
}
QuantLib::CTSMMCapletAlphaFormCalibration& Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletAlphaFormCalibration::GetReference ()
{
    if (_ppCTSMMCapletAlphaFormCalibration == NULL) throw REFNEW NativeNullException ();
	return **_ppCTSMMCapletAlphaFormCalibration;
}
boost::shared_ptr<QuantLib::CTSMMCapletAlphaFormCalibration>& Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletAlphaFormCalibration::GetShared ()
{
    if (_ppCTSMMCapletAlphaFormCalibration == NULL) throw REFNEW NativeNullException ();
	return *_ppCTSMMCapletAlphaFormCalibration;
}
QuantLib::CTSMMCapletAlphaFormCalibration* Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletAlphaFormCalibration::GetPointer ()
{
    if (_ppCTSMMCapletAlphaFormCalibration == NULL) throw REFNEW NativeNullException ();
	return &**_ppCTSMMCapletAlphaFormCalibration;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CTSMMCapletAlphaFormCalibration>& Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletAlphaFormCalibration::GetHandle ()
{
	if (_phCTSMMCapletAlphaFormCalibration == NULL)
	{
		_phCTSMMCapletAlphaFormCalibration = new Handle<QuantLib::CTSMMCapletAlphaFormCalibration> (*_ppCTSMMCapletAlphaFormCalibration);
	}
	return *_phCTSMMCapletAlphaFormCalibration;
}
#endif
bool Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletAlphaFormCalibration::HasNative () 
{
	return (_ppCTSMMCapletAlphaFormCalibration != NULL);
}

Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletAlphaFormCalibration::Alpha::get ()
{
    try
    {
    	std::vector<QuantLib::Real>& _rv = (std::vector<QuantLib::Real>&)(*_ppCTSMMCapletAlphaFormCalibration)->alpha ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt32 Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletAlphaFormCalibration::CapletAlphaFormCalibration (Cephei::QL::Models::Marketmodels::IEvolutionDescription^ evolution, Cephei::QL::Models::Marketmodels::IPiecewiseConstantCorrelation^ corr, Cephei::Core::IVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ displacedSwapVariances, Cephei::Core::IVector<Double>^ capletVols, Cephei::QL::Models::Marketmodels::ICurveState^ cs, Double displacement, Cephei::Core::IVector<Double>^ alphaInitial, Cephei::Core::IVector<Double>^ alphaMax, Cephei::Core::IVector<Double>^ alphaMin, Boolean maximizeHomogeneity, Cephei::QL::Models::Marketmodels::Models::IAlphaForm^ parametricForm, UInt64 numberOfFactors, Int32 steps, Double toleranceForAlphaSolving, Cephei::Core::IVector<Double>^ alpha, Cephei::Core::IVector<Double>^ a, Cephei::Core::IVector<Double>^ b, Cephei::Core::IVector<Cephei::QL::Math::IMatrix^>^ swapCovariancePseudoRoots)
{
    CEvolutionDescription^ _Cevolution;
    CPiecewiseConstantCorrelation^ _Ccorr;
    CCurveState^ _Ccs;
    CAlphaForm^ _CparametricForm;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cevolution = safe_cast<CEvolutionDescription^> (evolution);
        _Cevolution->Lock();
        QuantLib::EvolutionDescription& _evolution = static_cast<QuantLib::EvolutionDescription&> (_Cevolution->GetReference ()); 
        _Ccorr = safe_cast<CPiecewiseConstantCorrelation^> (corr);
        _Ccorr->Lock();
        QuantLib::PiecewiseConstantCorrelation& _corr = static_cast<QuantLib::PiecewiseConstantCorrelation&> (_Ccorr->GetReference ()); 
        displacedSwapVariances->Lock();
        INativeVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ _NCIdisplacedSwapVariances = displacedSwapVariances->getFeature (NativeFeature::shared_ptr);
        CPiecewiseConstantVarianceVector^ _NCdisplacedSwapVariances = safe_cast<CPiecewiseConstantVarianceVector^>(_NCIdisplacedSwapVariances);
        std::vector<boost::shared_ptr<
                QuantLib::PiecewiseConstantVariance> >& _displacedSwapVariances = static_cast<std::vector<boost::shared_ptr<
                QuantLib::PiecewiseConstantVariance> >&> (_NCdisplacedSwapVariances->GetShared ());
        capletVols->Lock();
        INativeVector<Double>^ _NCIcapletVols = capletVols->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCcapletVols = safe_cast<CDoubleVector^>(_NCIcapletVols);
        std::vector<QuantLib::Volatility>& _capletVols = static_cast<std::vector<QuantLib::Volatility>&> (_NCcapletVols->GetReference ());
        _Ccs = safe_cast<CCurveState^> (cs);
        _Ccs->Lock();
        QuantLib::CurveState& _cs = static_cast<QuantLib::CurveState&> (_Ccs->GetReference ()); 
        QuantLib::Spread _displacement = (QuantLib::Spread)ValueHelper::Convert (displacement); //a
        alphaInitial->Lock();
        INativeVector<Double>^ _NCIalphaInitial = alphaInitial->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCalphaInitial = safe_cast<CDoubleVector^>(_NCIalphaInitial);
        std::vector<QuantLib::Real>& _alphaInitial = static_cast<std::vector<QuantLib::Real>&> (_NCalphaInitial->GetReference ());
        alphaMax->Lock();
        INativeVector<Double>^ _NCIalphaMax = alphaMax->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCalphaMax = safe_cast<CDoubleVector^>(_NCIalphaMax);
        std::vector<QuantLib::Real>& _alphaMax = static_cast<std::vector<QuantLib::Real>&> (_NCalphaMax->GetReference ());
        alphaMin->Lock();
        INativeVector<Double>^ _NCIalphaMin = alphaMin->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCalphaMin = safe_cast<CDoubleVector^>(_NCIalphaMin);
        std::vector<QuantLib::Real>& _alphaMin = static_cast<std::vector<QuantLib::Real>&> (_NCalphaMin->GetReference ());
        bool _maximizeHomogeneity = (bool)ValueHelper::Convert (maximizeHomogeneity); //a
        _CparametricForm = safe_cast<CAlphaForm^> (parametricForm);
        _CparametricForm->Lock();
        boost::shared_ptr<QuantLib::AlphaForm> _parametricForm = static_cast<boost::shared_ptr<QuantLib::AlphaForm>> (_CparametricForm->GetShared ()); 
        QuantLib::Size _numberOfFactors = (QuantLib::Size)ValueHelper::Convert (numberOfFactors); //a
        QuantLib::Integer _steps = (QuantLib::Integer)ValueHelper::Convert (steps); //a
        QuantLib::Real _toleranceForAlphaSolving = (QuantLib::Real)ValueHelper::Convert (toleranceForAlphaSolving); //a
        alpha->Lock();
        INativeVector<Double>^ _NCIalpha = alpha->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCalpha = safe_cast<CDoubleVector^>(_NCIalpha);
        std::vector<QuantLib::Real>& _alpha = static_cast<std::vector<QuantLib::Real>&> (_NCalpha->GetReference ());
        a->Lock();
        INativeVector<Double>^ _NCIa = a->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCa = safe_cast<CDoubleVector^>(_NCIa);
        std::vector<QuantLib::Real>& _a = static_cast<std::vector<QuantLib::Real>&> (_NCa->GetReference ());
        b->Lock();
        INativeVector<Double>^ _NCIb = b->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCb = safe_cast<CDoubleVector^>(_NCIb);
        std::vector<QuantLib::Real>& _b = static_cast<std::vector<QuantLib::Real>&> (_NCb->GetReference ());
        swapCovariancePseudoRoots->Lock();
        INativeVector<Cephei::QL::Math::IMatrix^>^ _NCIswapCovariancePseudoRoots = swapCovariancePseudoRoots->getFeature (NativeFeature::Value);
        CMatrixVector^ _NCswapCovariancePseudoRoots = safe_cast<CMatrixVector^>(_NCIswapCovariancePseudoRoots);
        std::vector<QuantLib::Matrix>& _swapCovariancePseudoRoots = static_cast<std::vector<QuantLib::Matrix>&> (_NCswapCovariancePseudoRoots->GetReference ());
    	QuantLib::Natural _rv = (QuantLib::Natural)(*_ppCTSMMCapletAlphaFormCalibration)->capletAlphaFormCalibration ( _evolution,  _corr,  _displacedSwapVariances,  _capletVols,  _cs,  _displacement,  _alphaInitial,  _alphaMax,  _alphaMin,  _maximizeHomogeneity,  _parametricForm,  _numberOfFactors,  _steps,  _toleranceForAlphaSolving,  _alpha,  _a,  _b,  _swapCovariancePseudoRoots );   
        UInt32 _nrv = (UInt32)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cevolution != nullptr) _Cevolution->Unlock();
        if (_Ccorr != nullptr) _Ccorr->Unlock();
        if (displacedSwapVariances != nullptr) displacedSwapVariances->Unlock(); //NO2  
        if (capletVols != nullptr) capletVols->Unlock(); //NO2  
        if (_Ccs != nullptr) _Ccs->Unlock();
        if (alphaInitial != nullptr) alphaInitial->Unlock(); //NO2  
        if (alphaMax != nullptr) alphaMax->Unlock(); //NO2  
        if (alphaMin != nullptr) alphaMin->Unlock(); //NO2  
        if (_CparametricForm != nullptr) _CparametricForm->Unlock();
        if (alpha != nullptr) alpha->Unlock(); //NO2  
        if (a != nullptr) a->Unlock(); //NO2  
        if (b != nullptr) b->Unlock(); //NO2  
        if (swapCovariancePseudoRoots != nullptr) swapCovariancePseudoRoots->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Models::ICTSMMCapletAlphaFormCalibration^ Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletAlphaFormCalibration_Factory::Create (Cephei::QL::Models::Marketmodels::IEvolutionDescription^ evolution, Cephei::QL::Models::Marketmodels::IPiecewiseConstantCorrelation^ corr, Cephei::Core::IVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ displacedSwapVariances, Cephei::Core::IVector<Double>^ capletVols, Cephei::QL::Models::Marketmodels::ICurveState^ cs, Double displacement, Cephei::Core::IVector<Double>^ alphaInitial, Cephei::Core::IVector<Double>^ alphaMax, Cephei::Core::IVector<Double>^ alphaMin, Boolean maximizeHomogeneity, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Models::Marketmodels::Models::IAlphaForm^>^ parametricForm)
{
    return REFNEW CCTSMMCapletAlphaFormCalibration ( evolution,  corr,  displacedSwapVariances,  capletVols,  cs,  displacement,  alphaInitial,  alphaMax,  alphaMin,  maximizeHomogeneity,  parametricForm);
}
